package com.dragon.loan.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dragon.account.repository.PrCodeTableRepository;
import com.dragon.account.repository.SmSystemParamRepository;
import com.dragon.account.service.MpProductService;
import com.dragon.loan.entity.CommonParam;
import com.dragon.loan.entity.CommonParamItem;
import com.fintech.common.util.RedisUtils;
import com.marvels.common.utils.MarvelsLogUtil;

@Service
public class GetCommonParamService implements GetCommonParamIService {

	@Autowired
	private PrCodeTableRepository codeTableRepository;
	
	@Autowired
	private MpProductService mpProductService;
	
	@Autowired
	private SmSystemParamRepository smSystemParamRepository;
	

	@Override
	public CommonParam getCommonParam(String typeId) {
		if(!StringUtils.hasText(typeId)){
			return null;
		}
		return codeTableRepository.selectCommonParamByTypeId(typeId);
	}

	
	
	/**
	 * 获取产品期数列表
	 * @return
	 */
	public List<Map<String,Object>> getProductTenor(){
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		//查询所有产品类型
		CommonParam commonParam = getCommonParam("P2P_PRD_TYPE");
		if(null == commonParam){
			return list;
		}
		List<CommonParamItem> commonParamItems = commonParam.getCommonParamItems();
		for(CommonParamItem paramItem : commonParamItems){
			List<Integer> tenorList = mpProductService.selectProductTenor(paramItem.getItemId());
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("productType", paramItem.getItemId());
			map.put("tenorList", tenorList);
			list.add(map);
		}
		return list;
	}

	
	@Override
	public List<String> getPrCodeTableIdsByTypeIdUseCache(String typeId) {
		if(StringUtils.isEmpty(typeId)){
			return new ArrayList<>();
		}
		// 从redis中查询是否命中
		String idJson = null;
		try {
			idJson = RedisUtils.getInstance().get(typeId);
			if(org.apache.commons.lang3.StringUtils.isNotEmpty(idJson)){
				List<String> ids = JSONObject.parseArray(idJson, String.class);
				return ids;
			}
		} catch (Exception e) {
			RedisUtils.getInstance().del(typeId);
			MarvelsLogUtil.getInstance().error("=============取redis解析数据出现异常=============typeId = " + typeId + "ids = " + idJson, e);
		}
		// 如果没有命中查询数据库然后放入redis中
		List<String> resultList = codeTableRepository.selectIdByTypeId(typeId);
		if(org.apache.commons.collections.CollectionUtils.isEmpty(resultList)){
			return new ArrayList<>();
		}
		try {
			// 设置进redis 时间10分钟
			RedisUtils.getInstance().setex(typeId, 600, JSON.toJSONString(resultList));
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("============redis设置值异常=============", e);
		}
		return resultList;
	}

	@Override
	public String getTypeNameByTypeId(String typeId) {
		return codeTableRepository.getTypeNameByTypeId(typeId);
	}
	
}
